package com.linln.modules.system.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.linln.common.utils.HttpServletUtil;
import com.linln.modules.system.domain.Upload;
import com.linln.modules.system.repository.UploadRepository;
import com.linln.modules.system.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author 小懒虫
 * @date 2018/11/02
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private UploadRepository uploadRepository;

    @Value("${server.port}")
    private String port;

    /**
     * 获取文件sha1值的记录
     */
    @Override
    public Upload getBySha1(String sha1) {
        return uploadRepository.findBySha1(sha1);
    }

    /**
     * 保存文件上传
     * @param upload 文件上传实体类
     */
    @Override
    public Upload save(Upload upload){
        return uploadRepository.save(upload);
    }

    @Override
    public Upload inserts(MultipartFile file,String fileSavePath) throws IOException {
        Upload upload = new Upload();
        upload.setMime(file.getContentType());
        upload.setSize(file.getSize());
        upload.setName(file.getOriginalFilename());

        String name = uploadFile(file, fileSavePath);
        upload.setPath("/api/uploads/images/"+name);
        upload.setPort(port);
        uploadRepository.save(upload);
        return upload;
    }


    public String uploadFile(MultipartFile file, String fileSavePath) throws IOException {
        //生成文件的唯一id
        String fileId = IdWorker.getIdStr();

        //获取文件后缀
        String fileSuffix = getFileSuffix(file.getOriginalFilename());
        //定义一个后缀数组
        String[] arrs = {"png", "doc", "docx", "gif", "jpg", "jpeg", "pdf", "mp3", "mp4","rar","xls","xlsx","apk"};
        //判断文件后缀是否在后缀数组中
        boolean contains = Arrays.toString(arrs).contains(fileSuffix);
        if (contains){
            //获取文件原始名称
            String originalFilename = file.getOriginalFilename();

            //生成文件的最终名称
            String finalName = fileId + "." + getFileSuffix(originalFilename);

            try {
                //保存文件到指定目录
                File newFile = new File(fileSavePath + finalName);

                //创建父目录
                FileUtil.mkParentDirs(newFile);

                //保存文件
                file.transferTo(newFile);


            } catch (Exception e) {

            }
            return finalName;
        }else{
            throw new RuntimeException("文件格式不正确");
        }
    }
    public static String getFileSuffix(String fileWholeName) {
        if (StringUtils.isEmpty(fileWholeName)) {
            return "none";
        } else {
            int lastIndexOf = fileWholeName.lastIndexOf(".");
            return fileWholeName.substring(lastIndexOf + 1);
        }
    }


}

